package com.zhang.zhangapp.view.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.shop.Evaluate;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.adapter.CommentAdapter;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 罗肖 on 2018/4/20.
 */

@ContentView(R.layout.activity_comment_list)
public class CommentListActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.refresh_layout)
    PullToRefreshLayout refreshLayout;
    @ViewInject(R.id.comment_listview)
    ListView listView;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    List<Evaluate> evaluateList = new ArrayList<>();
    private String shopCode;
    CommentAdapter adapter;
    Map<String, String> params = new HashMap<>();

    MCallback callback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            Gson gson = new Gson();
            List<Evaluate> list = gson.fromJson(data, new TypeToken<List<Evaluate>>() {
            }.getType());
            if (list != null) {
                evaluateList.addAll(list);
                if (list.size() < BaseConfig.PAGE_SIZE) {
                    refreshLayout.setCanLoadMore(false);
                }
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onSuccess(String message, String code) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
            dismisionDalog();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        shopCode = getIntent().getStringExtra("shopCode");
        x.view().inject(this);
        initView();
        getComments(1);
    }

    private void initView() {
        titleText.setText("全部评论");
        adapter = new CommentAdapter(evaluateList, this);
        listView.setAdapter(adapter);
        params.put("shopCode", shopCode);
        params.put("pageSize", BaseConfig.PAGE_SIZE + "");
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                getComments(1);
            }

            @Override
            public void loadMore() {
                getComments((evaluateList.size() / BaseConfig.PAGE_SIZE + 1));
            }
        });
    }

    /**
     * get comment list
     *
     * @param pageNo
     */
    private void getComments(int pageNo) {
        if (pageNo == 1) {
            refreshLayout.setCanLoadMore(true);
            evaluateList.clear();
            adapter.notifyDataSetChanged();
        }
        if (showLoadingDialog()) {
            params.put("pageNum", pageNo + "");
            CommonHttp.commonHttp("hzg/shop/listEvaluate", params, callback);
        }
    }
}
